// /////////////////////////////////////////////////////////////////////////////
// r2p group ////////////////////////////////////////////////////////////////////
// /////////////////////////////////////////////////////////////////////////////
$.fn.getR2Pdata = function(_lotto_id,_gametype){
  $().disAreaObj();
 $.getJSON('op.php?op=xml_3d&fp=rate_3d&playtype=r2p&lotto_id_3d='+_lotto_id+"&gametype="+_gametype+"&disk="+ $("select#slcDisk").val() ,function(ratedata){
      var dataLen = 0;
      var urlPre = "";
      var hrefStr = "";
      if(ratedata.errormsg != undefined){
        $("div#errormsg").addClass("error").text("No Rate data");
      }else{
        $().creatRateData(ratedata.r14, '14',_gametype);
        $("div#errormsg").text("");
        $("input#chgr14").addClass("btnchoiced");
              
        // change play type mode by click //////////////////////////////////////
        $("input#chgr14").click(function(){
            $().creatRateData(ratedata.r14, '14',_gametype);
            $(this).addClass("btnchoiced");
        });
        $("input#chgr15").click(function(){
          $().creatRateData(ratedata.r15, '15',_gametype);
          $(this).addClass("btnchoiced");
        });
        $("input#chgr16").click(function(){
            $().creatRateData(ratedata.r16, '16',_gametype);
            $(this).addClass("btnchoiced");
        });
         
      }
      $().redisAreaObj();
  });
};

// create table data
$.fn.creatRateData = function(rdata, rnum,_gametype){
    //$("span#chgBtnGroup").fadeOut('fast');
    $("span#chgBtnGroup input:button").removeClass("btnchoiced").addClass("btnchoice");
    //$("table#tblNowBet").fadeOut('fast');
    $("div#errormsg").text("Loading...");
    
    $("tbody#tbdr2p").empty();
  
    dataLen = rdata.number.length;
    urlPre = "op.php?op=member_3d&fp=bet_fast&palygroup=r2p&bettype="+rnum+"&gametype="+_gametype+"&disk="+ $("select#slcDisk").val();
    var tempTr = 5; // cols of each rows
    var class2="";
    switch(rnum){
    case '14':
    class2="ball3d";
    $.fn.showNum = function(str){return str + "X";};
    break;
    case '15':
    class2="ball3d";
    $.fn.showNum = function(str){return str.substr(0,1) + "X" + str.substr(1,1);};
    break;
    case '16':
    class2="ball3d";
    $.fn.showNum = function(str){return "X" + str;};
    break;
    }
    for(i = 0; i < dataLen; i++){
      
      hrefStr = urlPre + "&num=" + rdata.number[i] + "&rate=" + rdata.rates[i];
      if(i == 0){
          $('<tr class="td2" align=center><th class="title02_3d '+class2+'">'+$().showNum(rdata.number[i])+'</th><td class="betnum"><a href="'+hrefStr+'" class="betRateNum" target="leftFrame">'+rdata.rates[i]+'</a></td><td><input type="text" size="3" name="r'+rnum+'_'+rdata.number[i]+'" /></td></tr>').appendTo("tbody#tbdr2p");
      }else if(i > 0 && i % tempTr == 0){
        $('<tr class="td2" align=center><th class="title02_3d '+class2+'">'+$().showNum(rdata.number[i])+'</th><td class="betnum"><a href="'+hrefStr+'" class="betRateNum" target="leftFrame">'+rdata.rates[i]+'</a></td><td><input type="text" size="3" name="r'+rnum+'_'+rdata.number[i]+'" /></td></tr>').appendTo("tbody#tbdr2p");
      }else{
        $('<th class="title02_3d '+class2+'" align=center>'+$().showNum(rdata.number[i])+'</th><td class="betnum"><a href="'+hrefStr+'" class="betRateNum" target="leftFrame">'+rdata.rates[i]+'</a></td><td><input type="text" size="3" name="r'+rnum+'_'+rdata.number[i]+'" /></td>').appendTo("tbody#tbdr2p tr:last");
      }
    }
    
    //$("table#tblNowBet").fadeIn('slow');
    $("div#errormsg").text("");
    //$("span#chgBtnGroup").fadeIn('slow');
    
    // change text or checkbox type
    var timebtnType = $("input:hidden#timebtnType").val();
    if(timebtnType == "checkbox"){
      $().timebtnTypeToCheckbox(); 
    }
};

// set 0-9 numbers
$.fn.getNum = function(num){
   var tmpName = "";
   var tmp1 = "";
   var tmp2 = "";
  
   var timebtnType = $("input:hidden#timebtnType").val();
             
   $("table#tblNowBet input:"+timebtnType).val(""); 
   $("table#tblNowBet input:"+timebtnType).filter(function(){
      tmpName = $(this).attr("name");
      tmp1 = tmpName.substr(4,1);
      tmp2 = tmpName.substr(5,1);
      if(tmp1 == tmp2){
        return false;
      }else if(tmp1==num || tmp2 == num){
        return true;
      }
   }).val("*");
   if(timebtnType=="checkbox"){
    $("table#tblNowBet input:checkbox[name^='r']:checked").removeAttr("checked");
    $("table#tblNowBet input:checkbox[name^='r'][value='*']").attr("checked","checked");
  }

};

// set pair numbers
$.fn.getPairNum = function(num){
   var tmpName = "";
   var tmp1 = "";
   var tmp2 = "";
   var timebtnType = $("input:hidden#timebtnType").val();
   $("table#tblNowBet input:"+timebtnType).val(""); 
   $("table#tblNowBet input:"+timebtnType).filter(function(){
      tmpName = $(this).attr("name");
      tmp1 = tmpName.substr(4,1);
      tmp2 = tmpName.substr(5,1);
      if(tmp1 == tmp2){
        return true;
      }else{
        return false;
      }
   }).val("*");
    if(timebtnType=="checkbox"){
  
      $("table#tblNowBet input:checkbox[name^='r']:checked").removeAttr("checked");
      $("table#tblNowBet input:checkbox[name^='r'][value='*']").attr("checked","checked");
    }

};




// renew the rate at r2
$.fn.renewR2data = function(_lotto_id){
 $.getJSON('op.php?op=xml_3d&fp=rate_3d&playtype=r1&lotto_id_3d='+_lotto_id ,function(ratedata){
      var dataLen = 0;
      var urlPre = "";
      var hrefStr = "";
      if(ratedata.errormsg != undefined){
        $("div#errormsg").addClass("error").text("No Rate data");
      }else{
        
        // check rate bettype index 
        $().checkRateChg("r1",ratedata.r1);
        $().checkRateChg("r2",ratedata.r2);
        $().checkRateChg("r3",ratedata.r3);
        $().checkRateChg("r0",ratedata.r0);
        
        $().checkRateChg("r4",ratedata.r4);
        $().checkRateChg("r5",ratedata.r5);
        $().checkRateChg("r6",ratedata.r6);
        $().checkRateChg("r7",ratedata.r7);
        $().checkRateChg("r8",ratedata.r8);
        $().checkRateChg("r9",ratedata.r9);
        $().checkRateChg("r10",ratedata.r10);
        $().checkRateChg("r11",ratedata.r11);
        $().checkRateChg("r12",ratedata.r12);
        
        $().checkRateChg("r22",ratedata.r22);
        $().checkRateChg("r23",ratedata.r23);
        
        // renew again
        renewRateTimeObj = setTimeout(function(){$().renewR1data(_lotto_id);}, renewRateTime);
      }  
    
  });
};


// check rate
$.fn.checkRateChg = function(tbdId,ratedata){
  var dataLen = ratedata.number.length;
  var tbd = "tbody#" + tbdId;
  $(tbd + " td[class=betnum alert1]").removeClass("alert1");
  for(i = 0; i < dataLen; i++){
    if($(tbd + " tr:eq("+i+")>td>a:first").text() != ratedata.rates[i]){
      $(tbd + " tr:eq("+i+")>td>a:first").text(ratedata.rates[i]).parent("td").addClass("alert1");
    }
  }
  

}; 

